<?php

require_once 'tView/TestSuite.php';

class tView {
	
	/**
	 * Parsed XML file with testing results.
	 *
	 * @var object
	 */
	protected $_xmlReport;
	
	/**
	 * Test suites.
	 *
	 * @var array
	 */
	protected $_testSuites = array();
		
	/**
	 * Name of given tests.
	 *
	 * @var string
	 */
	protected $_testName;
		
	/**
	 * All asserts count.
	 *
	 * @var int
	 */
	protected $testAsserts = 0;
	
	/**
	 * All tests time.
	 *
	 * @var float
	 */
	protected $_testsTime = 0;
	
	/**
	 * Passed tests.
	 *
	 * @var array
	 */
	protected $_testsPassed = array();
	
	/**
	 * Failed tests.
	 *
	 * @var array
	 */
	protected $_testsFailed = array();
	
	/**
	 * Error tests.
	 *
	 * @var array
	 */
	protected $_testsError = array();
	
	/**
	 * Empty tests.
	 *
	 * @var array
	 */
	protected $_testsEmpty = array();
	
	public function __construct() {
		if (file_exists(tView_Config::$xmlFile)) {
			$this->_xmlReport = simplexml_load_file(tView_Config::$xmlFile);
		} else {
			// TODO Сделать нормальную обработку ошибки.
			exit('Failed to open file with testing results.');
		}
		
		$this->_testName = (string)$this->_xmlReport->{'testsuite'}['name'];
		
		foreach ($this->_xmlReport->{'testsuite'}->{'testsuite'} as $testSuite) {
			$_testSuite = new tView_TestSuite($testSuite);
			if (!$_testSuite->isEmpty()) {
				foreach ($testSuite->{'testcase'} as $testCase) {
					$_testSuite->addTestCase($testCase);
				}
			}
			switch ($_testSuite->getType()) {
				case tView_TestSuite::TYPE_PASSED : 
					$this->_testsPassed[] = $_testSuite;
					break;
				case tView_TestSuite::TYPE_EMPTY :
					$this->_testsEmpty[] = $_testSuite;
					break;
				case tView_TestSuite::TYPE_ERROR :
					$this->_testsError[] = $_testSuite;
					break;
				default :
					$this->_testsFailed[] = $_testSuite;
					break;
			}

			$this->_testsTime += $_testSuite->getTime();
		}
	}
	
	public function getName() {
		return $this->_testName;
	}
	
	public function getCount() {
		return $this->getPassedCount()
			+ $this->getEmptyCount()
			+ $this->getErrorCount()
			+ $this->getFailedCount();
	}
	
	public function getPassed() {
		return $this->_testsPassed;
	}
	
	public function getPassedCount() {
		return count($this->_testsPassed);
	}
	
	public function getEmpty() {
		return $this->_testsEmpty;
	}
	
	public function getEmptyCount() {
		return count($this->_testsEmpty);
	}
	
	public function getError() {
		return $this->_testsError;
	}
	
	public function getErrorCount() {
		return count($this->_testsError);
	}
	
	public function getFailed() {
		return $this->_testsFailed;
	}
	
	public function getFailedCount() {
		return count($this->_testsFailed);
	}
	
	public function getTime() {
		return $this->_testsTime;
	}
	
	
	
	/**
	 * RENDER FUNCTIONS
	 */
	
	public function renderBar() {
		$xhtml = '';
		
		$testsPassed = $this->getPassedCount();
		$testsEmpty = $this->getEmptyCount();
		$testsError = $this->getErrorCount();
		$testsFailed = $this->getFailedCount();
		
		$coef = $this->getCount() / 300;
		
		if ($testsPassed > 0) {
			$xhtml .= '<img src="img/barPassLeft.png" height="20" width="10" />';  
		} elseif ($testsEmpty > 0) {
			$xhtml .= '<img src="img/barEmptyLeft.png" height="20" width="10" />';  	
		} elseif ($testsError > 0) {
			$xhtml .= '<img src="img/barErrorLeft.png" height="20" width="10" />';  	
		} else {
			$xhtml .= '<img src="img/barFailLeft.png" height="20" width="10" />';  	
		}
					
		if ($testsPassed > 0) {
			$xhtml .= '<img src="img/barPassUnit.png" height="20" width="'
				. floor($testsPassed / $coef)
				. '" />';
		}
		if ($testsEmpty > 0) {
			$xhtml .= '<img src="img/barEmptyUnit.png" height="20" width="'
				. floor($testsEmpty / $coef)
				. '" />';
		}
		if ($testsError > 0) {
			$xhtml .= '<img src="img/barErrorUnit.png" height="20" width="'
				. floor($testsError / $coef)
				. '" />';
		}
		if ($testsFailed > 0) {
			$xhtml .= '<img src="img/barFailUnit.png" height="20" width="'
				. floor($testsFailed / $coef)
				. '" />';
		}
					
		if ($testsFailed > 0) {
			$xhtml .= '<img src="img/barFailRight.png" height="20" width="10" />';  	
		} elseif ($testsError > 0) {
			$xhtml .= '<img src="img/barErrorRight.png" height="20" width="10" />';  	
		} elseif ($testsEmpty > 0) {
			$xhtml .= '<img src="img/barEmptyRight.png" height="20" width="10" />';  	
		} else {
			$xhtml .= '<img src="img/barPassRight.png" height="20" width="10" />';  	
		}
		
		return $xhtml;
	}
	
}
